package jh.com.jhappbf.finance;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.ab.activity.AbActivity;
import com.ab.view.titlebar.AbTitleBar;
import com.android.volley.Request.Priority;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.core.content.ContextCompat;
import jh.com.jhappbf.R;
import jh.com.jhappbf.httputils.VolleyCallWebService;
import jh.com.jhappbf.httputils.VolleySingleton;
import jh.com.jhappbf.ui.State;
import jh.com.jhappbf.utils.JsonHelper;

/**
 * @author Administrator
 */
public class SettleAccountActivity extends AbActivity implements OnClickListener {

    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.finance_settleaccount);
        AbTitleBar mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText("添加结算");
        mAbTitleBar.setLogo(R.drawable.button_selector_back);
        mAbTitleBar.setTitleBarBackgroundColor(ContextCompat.getColor(this, R.color.orange));
        mAbTitleBar.setTitleTextMargin(10, 0, 0, 0);
        mAbTitleBar.setLogoLine(R.mipmap.line);
        initView();
    }

    private void initView() {
        editText = findViewById(R.id.finance_settleAccountMsg);
        Button btnOk = findViewById(R.id.finance_btn_ok);
        Button btnRefresh = findViewById(R.id.finance_btn_refresh);
        btnOk.setOnClickListener(this);
        btnRefresh.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.finance_btn_ok:
                settleAccount();//结算
                break;
            case R.id.finance_btn_refresh:
                clearInput();

                break;
            default:
        }
    }

    /**
     * 刷新
     */
    private void clearInput() {
        editText.setText("");

    }

    /**
     * 结算
     */
    private void settleAccount() {

        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        String settleAccountMsg = editText.getText().toString();

        if (settleAccountMsg.isEmpty()) {
            Toast.makeText(getApplicationContext(), "结算信息不能为空！", Toast.LENGTH_SHORT).show();
            return;
        }
        String methodName = "settleAccount";
        ArrayList<String> param = new ArrayList<>();
        ArrayList<String> value = new ArrayList<>();
        param.add("id");
        param.add("settleAccountMsg");
        param.add("settleMan");
        value.add(id);
        value.add(settleAccountMsg);
        value.add(State.mName);

        Listener<String> listener = response -> {
            List<JSONObject> list = JsonHelper.getJsonObjects(response);
            JSONObject obj = list.get(0);
            String result = obj.optString("result");
            if ("true".equals(result)) {
                String memo = obj.optString("memo");
                Toast.makeText(SettleAccountActivity.this, memo, Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(SettleAccountActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
            }
        };
        ErrorListener errorListener = volleyError -> Toast.makeText(SettleAccountActivity.this, "操作失败！", Toast.LENGTH_SHORT).show();
        VolleyCallWebService webService = new VolleyCallWebService(methodName, param, value, listener, errorListener, Priority.NORMAL);
        VolleySingleton.getVolleyInstance(SettleAccountActivity.this).addRequest(webService);
    }
}
